package edu.predatorwars.server.host;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import edu.predatorwars.server.engine.GameEngine;
import edu.predatorwars.shared.models.*;

public class GameHostImpl implements GameHostInterface, Runnable {

	/*
	 * Acts as central host for players
	 */

	ArrayList<Player> playerList;
	ArrayList<GameInstance> gameList;

	final int numPlayers = 2;

	GameHostImpl() {
		gameList = new ArrayList<GameInstance>();
	}

	public void run() {
		new GameHostImpl();
	}

	public void createGame(Player joiningPlayer) {
		playerList = new ArrayList<Player>();
		playerList.add(joiningPlayer);
		startGame(new GameEngine(playerList));
	}


	public boolean joinGame(Player joiningPlayer, UUID gameId) {
		boolean joined = false;
		for (GameInstance game : gameList) {
			if (gameId.compareTo(game.getUUID()) == 0) {
				GameEngine engine = game.getGameEngine();
				joined = engine.addPlayer(joiningPlayer);
				if (engine.getMaxPlayers() == engine.getCurrentPlayers().size()) {
					engine.doAllPlayersAdded();
				}
			}
		}	
		return joined;
	}

	// WEB SERVICE CLIENT CALLS
	public void selectCharacter(Player player, Creature creature, UUID gameId) {
		for (GameInstance game : gameList) {
			if (gameId.compareTo(game.getUUID()) == 0) {
				game.getGameEngine().setCreature(player, creature.getName());
			}
		}
	}

	public void quitGame(Player quittingPlayer, UUID gameId) {
		for (GameInstance game : gameList) {
			if (game.getUUID().compareTo(gameId) == 0) {
				game.getGameEngine().removePlayer(quittingPlayer);
			}
		}
	}

	private void startGame(GameEngine gameEngine) {
		GameInstance game = new GameInstance(this, UUID.randomUUID(),
				gameEngine);
		gameEngine.addGameListener(game); // The game is subscribed as a board
		// listener to the engine
		gameList.add(game); // Host now has the game in a list of games...right
		// now, no functionality to this
	}

	protected void latestUpdate(Message msg, List<Player> players) {
		PlayerConnectionInfo connection = null;
		String ip = "";
		// sends message payload to clients
		for (Player player : players) {
			connection = player.getCommInfo();
			ip = connection.getIpAddress();
			// TODO: have IP, now invoke client service with payload
		}
	}

	public void move(Player player, Move move, UUID gameId) { // WEB SERVICE
		// player makes move, redirect to game engine
		for (GameInstance game : gameList) {
			if (gameId.compareTo(game.getUUID()) == 0) {
				game.getGameEngine().doMove(player, move);
			}
		}
	}

	public void refreshGameList(Player refreshingPlayer) { //WEB SERVICE
		List<GameInfo> openGames = new ArrayList<GameInfo>();
		for (GameInstance game : gameList) {
			GameEngine engine = game.getGameEngine();
			if (engine.getGameState() == 0) { //0 means game is in setup stage
				GameInfo gameInfo = new GameInfo(game.getUUID(), engine.getMaxPlayers(), engine.getCurrentPlayers().size(), engine.getBoardName());
				openGames.add(gameInfo);
			}
		}
		Message msg = new GameListPayload(openGames);
		//create message
		ArrayList<Player> player = new ArrayList<Player>();
		player.add(refreshingPlayer);
		latestUpdate(msg, player);
	}
}